package com.xframe.common.config;

import com.xframe.domain.repository.SysBaseRepository;
import com.xframe.domain.sys.SysModelConfig;
import com.xframe.domain.ModelRepositoryFactory;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.FindOptions;
import com.xframe.xdal.core.serialize.IModelConfig;
import com.xframe.xdal.core.serialize.JsonConfigContainer;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.core.annotation.Order;

import java.util.List;

@Log4j2
@Order(value = 100)
@AutoConfiguration
public class LoadModelConfig implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("LoadModelConfig-------------------run");
        //查询数据库中的配置
        if(JsonConfigContainer.getConfigContainer() == null) {
            JsonConfigContainer.init();
        }
        if(JsonConfigContainer.getConfigContainer() != null) {
            FindOptions options = new FindOptions();
            options.getConditionValueCollection().add(new ConditionValue("delFlag", 0));
            options.getConditionValueCollection().add(new ConditionValue("configType", "ser"));
            SysBaseRepository repository = ModelRepositoryFactory.builder(SysModelConfig.class);

            List<SysModelConfig> modelViewList = repository.queryModle(SysModelConfig.class, options);

            for (IModelConfig item : modelViewList) {
                JsonConfigContainer.getConfigContainer().addConfigByNoContainsKey(item);
            }
        }
    }


}
